<template>
  <div>
    <!-- 表格显示 -->
    <el-table :data="exportOrders" style="width: 100%">
      <el-table-column prop="exitDate" label="出库时间" :formatter="formatDate"></el-table-column>
      <el-table-column prop="orderNumber" label="订单号"></el-table-column>
      <el-table-column prop="itemCount" label="出库件数"></el-table-column>
      <el-table-column prop="itemWeightTon" label="出库吨数"></el-table-column>
      <el-table-column prop="businessNumber" label="业务号"></el-table-column>
      <el-table-column prop="portId" label="港口ID"></el-table-column>
      <el-table-column prop="companyId" label="公司ID"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.exitId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增出库记录按钮 -->
    <el-button type="primary" @click="showAddDialog">新增出库记录</el-button>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="出库信息">
      <el-form :model="entryForm">
        <el-form-item label="出库时间">
          <el-date-picker v-model="entryForm.exitDate" value-format="YYYY-MM-DD" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="entryForm.orderNumber"></el-input>
        </el-form-item>
        <el-form-item label="出库件数">
          <el-input v-model="entryForm.itemCount" type="number"></el-input>
        </el-form-item>
        <el-form-item label="出库吨数">
          <el-input v-model="entryForm.itemWeightTon" type="number"></el-input>
        </el-form-item>
        <el-form-item label="业务号">
          <el-input v-model="entryForm.businessNumber"></el-input>
        </el-form-item>
        <el-form-item label="港口ID">
          <el-input v-model="entryForm.portId" type="number"></el-input>
        </el-form-item>
        <el-form-item label="公司ID">
          <el-input v-model="entryForm.companyId" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      exportOrders: [], // 出库记录列表
      dialogVisible: false, // 对话框是否显示
      entryForm: {
        exitDate: '',
        orderNumber: '',
        itemCount: '',
        itemWeightTon: '',
        businessNumber: '',
        portId: '',
        companyId: '',
      },
      isEditing: false, // 当前是否是编辑状态
    };
  },
  created() {
    this.fetchExportOrders(); // 初始化时获取出库记录
  },
  methods: {
    // 获取所有出库记录
    async fetchExportOrders() {
      try {
        const response = await axios.get('http://localhost:8080/storageexit/list');
        this.exportOrders = response.data;
      } catch (error) {
        console.error('获取出库记录失败:', error);
      }
    },
    // 显示新增对话框
    showAddDialog() {
      this.isEditing = false;
      this.entryForm = {
        exitDate: '',
        orderNumber: '',
        itemCount: '',
        itemWeightTon: '',
        businessNumber: '',
        portId: '',
        companyId: '',
      };
      this.dialogVisible = true;
    },
    // 编辑出库记录
    handleEdit(row) {
      this.isEditing = true;
      this.entryForm = {...row};
      this.dialogVisible = true;
    },
    // 保存出库记录（新增或编辑）
    async handleSave() {
      try {
        if (this.isEditing) {
          await axios.put('http://localhost:8080/storageexit/update', this.entryForm);
        } else {
          await axios.post('http://localhost:8080/storageexit/add', this.entryForm);
        }
        this.dialogVisible = false;
        this.fetchExportOrders(); // 重新获取出库记录
      } catch (error) {
        console.error('保存出库记录失败:', error);
      }
    },
    // 删除出库记录
    async handleDelete(id) {
      try {
        await axios.delete(`http://localhost:8080/storageexit/delete/${id}`);
        this.fetchExportOrders(); // 重新获取出库记录
      } catch (error) {
        console.error('删除出库记录失败:', error);
      }
    },
    // 格式化日期
    formatDate(row, column, cellValue) {
      return cellValue ? cellValue.split('T')[0] : '';
    }
  }
};
</script>

<style scoped>
/* 这里可以添加一些样式 */
</style>
